package com.carsys.service.Impl;

import com.carsys.entity.DetailedOrder;
import com.carsys.entity.Order;

import com.carsys.entity.Orderx;
import com.carsys.mapper.OrderMapper;
import com.carsys.service.OrderService;
import com.carsys.vo.VOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.carsys.mapper.OrderMapper;
import com.carsys.service.OrderService;
import com.carsys.utils.Order2DetailedOrderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired(required = false)
    private OrderMapper orderMapper;

    /**
     * 通过要查询的页面查询
     * @param page 要查询的页面
     * @param id 要查询的用户id
     * @return 返回订单列表
     */
    @Override
    public List<DetailedOrder> getOrderListByPageAndId(int page, int id) {
        page = (page -1)*3;
        List<Order> list = orderMapper.getOrderListByPageAndId(page,id);
        List<DetailedOrder> lists = new ArrayList<DetailedOrder>();
        for (Order order: list) {
            DetailedOrder detailedOrder = Order2DetailedOrderUtils.order2DetailedOrder(order,orderMapper,id);
            lists.add(detailedOrder);
        }
        return lists;
    }
   
    @Override
    public void addOrder(Integer uid, Integer cid, Integer getid, Integer backid, String oprices) {
        String status = "已预订";
        double oprice = Double.parseDouble(oprices);
        Orderx order = new Orderx();
        order.setUid(uid);
        order.setCid(cid);
        order.setDetid(getid);
        order.setBackid(backid);
        order.setOprice(oprice);
        order.setStatus(status);
        System.out.println(order + "1");
        if (orderMapper.insertOrder(order) > 0) {
            System.out.println("6666");
        }
        System.out.println(order);
    }

    @Override
    public List<VOrder> findAllOredrs() {
        return orderMapper.findAllOrders();
    }

    @Override
    public DetailedOrder getOrderById(int id) {
        Order order = orderMapper.getOrderById(id);
        DetailedOrder detailedOrder = Order2DetailedOrderUtils.order2DetailedOrder(order,orderMapper,id);
        return detailedOrder;
    }

    @Override
    public boolean delOrderById(int id) {
        int i = orderMapper.delOrderById(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean editOrder(DetailedOrder detailedOrder) {
        String status = detailedOrder.getOstatus();
        int id = detailedOrder.getOid();
        int i = orderMapper.editOrder(status,id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean delteOrderById(int id) {
        try {
            orderMapper.delteOrderById(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
